//
//  QLMServicemanListVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/31.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMServicemanListVC.h"
#import "QLMIntroManDetailTabCell.h"
#import "QLMServiceManDetailVC.h"

@interface QLMServicemanListVC ()<UITableViewDelegate,UITableViewDataSource>{
   QLMCustomTipView *tipView;
    NSInteger currentPage;
}
@property (nonatomic,strong) UITableView *servtabview;
@end

@implementation QLMServicemanListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"服务工程师列表";
    
    currentPage=1;
    _serviceInfoModel=[[QLMServiceManInfoModel alloc] init];
    _serviceInfoModel.data=[NSMutableArray array];
    
    CGRect rect=CGRectMake(0, 0, kScreen_width, kScreen_height-QLM_StatusBarAndNavigationBarHeight);
    _servtabview=[[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    _servtabview.backgroundColor=[UIColor clearColor];
    _servtabview.delegate=self;
    _servtabview.dataSource=self;
    _servtabview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _servtabview.showsVerticalScrollIndicator=NO;
    _servtabview.showsHorizontalScrollIndicator=NO;
    _servtabview.tableFooterView=[UIView new];
    [self.view addSubview:_servtabview];
    
    //上拉刷新
    _servtabview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestServiceManData)];
    _servtabview.mj_footer.automaticallyHidden = YES;
    
    tipView=[QLMCustomTipView sharedInstance];
    tipView.hidden = YES;
    [_servtabview addSubview:tipView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    
    [self requestServiceManData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [_serviceInfoModel.data removeAllObjects];
    currentPage=1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestServiceManData{
    
    NSDictionary *enddic=[[NSUserDefaults standardUserDefaults] objectForKey:@"enddic"];
    if ([enddic allKeys].count==0){
        return;
    }
    double latitude = [enddic[@"latitu"] doubleValue];
    double longtitude = [enddic[@"longtitu"] doubleValue];
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSString *latStr=[NSString stringWithFormat:@"%.6f",latitude];
    NSString *longStr=[NSString stringWithFormat:@"%.6f",longtitude];
    
    [dic setObject:latStr forKey:@"lat1"];
    [dic setObject:longStr forKey:@"lng1"];
    
    [dic setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"pageSize"];
    [dic setObject:@"10" forKey:@"rows"];
    
    [ClientTool postUrlPath:@"/rest/serviceEvaluation/getAll" withParamers:dic andBody:nil
                    success:^(id responseObject){
                        [_servtabview.mj_footer endRefreshing];
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                            
                            [_serviceInfoModel.data addObjectsFromArray:responseObject[@"data"]];
                            
                            if (_serviceInfoModel.data.count==0) {
                                tipView.hidden = NO;
                            }else{
                                if ([responseObject[@"data"] count]<10) {
                                    _servtabview.mj_footer.state = MJRefreshStateNoMoreData;
                                }
                                tipView.hidden = YES;
                                currentPage++;
                            }
                            
                            [_servtabview reloadData];
                            
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                        
                    }failure:^(NSError *error){
                        ETToast(@"服务器连接失败");
                        [_servtabview.mj_footer endRefreshing];
                    }
     ];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITableView datasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc] init];
    headerView.backgroundColor=[UIColor clearColor];
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _serviceInfoModel.data.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QLMIntroManDetailTabCell *cell=[QLMIntroManDetailTabCell cellWithTableView:tableView];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    if (_serviceInfoModel.data.count!=0) {
        
        QLMServiceDetailModel *serDetailModel=[[QLMServiceDetailModel alloc] initWithDictionary:_serviceInfoModel.data[indexPath.row] error:nil];
        [cell.compaynameBtn setTitle:serDetailModel.companyName forState:UIControlStateNormal];
        cell.usernameLabel.text=serDetailModel.servicerUserName;
        
        NSString *picStr=[NSString stringWithFormat:@"%@%@",kBaseImageUrl,serDetailModel.iconPath];
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:[UIImage imageNamed:@"header"]];
        
        NSString *customerEvaluationNumber=nil;
        if (serDetailModel.customerEvaluationNumber.length==0) {
            customerEvaluationNumber=@"无";
        }else{
            customerEvaluationNumber=serDetailModel.customerEvaluationNumber;
        }
        
        NSString *scoreStr=[NSString stringWithFormat:@"用户评价：%@",customerEvaluationNumber];
        NSMutableAttributedString *aString=[[NSMutableAttributedString alloc] initWithString:scoreStr];
        [aString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xffb910) range:NSMakeRange(5, scoreStr.length-5)];
        
        NSString *levelStr=[NSString stringWithFormat:@"技能星级：%@",serDetailModel.servicerLevelNum];
        NSMutableAttributedString *bString=[[NSMutableAttributedString alloc] initWithString:levelStr];
        [bString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xffb910) range:NSMakeRange(5, levelStr.length-5)];
        
        cell.customerScoreLabel.attributedText=aString;
        cell.techScoreLabel.attributedText=bString;
    }
    
    return cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QLMServiceDetailModel *serDetailModel=[[QLMServiceDetailModel alloc] initWithDictionary:_serviceInfoModel.data[indexPath.row] error:nil];
    
    QLMServiceManDetailVC *serDetailVC=[QLMServiceManDetailVC new];
    serDetailVC.serDetailModel=serDetailModel;
    serDetailVC.isScanInfoBool=YES;
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:serDetailVC animated:YES];
}

@end
